package com.iot08.api.service;

import com.iot08.api.vo.request.StatisticsReqVO;
import com.iot08.common.constant.Const;
import com.iot08.common.entity.app.StatisticsList;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.DateUtil;
import com.iot08.common.utils.StringUtil;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @version 1.0
 * @Description:
 * @author: Rock
 * @time: 2020/12/23 19:03
 */
@Slf4j
@Service
public class StatisticsListService extends BaseService<StatisticsList> {


    /**
     * @param cmd
     * @param openid
     * @param jumpUrl
     * @param smallprogramId
     * @param smallprogramPath
     * @time: 2020/12/26 9:17
     * @author: Rock
     * @version 1.0
     */
    public void add(String cmd, String openid, String jumpUrl, String smallprogramId, String smallprogramPath) {
        StatisticsList statisticsList = new StatisticsList();
        if (StringUtil.isNotBlank(jumpUrl)) {
            statisticsList.setJumpUrl(jumpUrl);
        }
        if (StringUtil.isNotBlank(smallprogramId)) {
            statisticsList.setSmallprogramId(smallprogramId);
        }
        if (StringUtil.isNotBlank(smallprogramPath)) {
            statisticsList.setSmallprogramPath(smallprogramPath);
        }
        statisticsList.setCmd(cmd);
        statisticsList.setOpenid(openid);
        statisticsList.setExpiryAt(DateUtil.getSystemExpiryAt());
        statisticsList.setCreatetime(new Date());
        statisticsList.setTime(DateUtil.nowTimestampToDouble());
        this.save(statisticsList);
    }

    /**
     * @param vo
     * @time: 2020/12/26 9:23
     * @author: Rock
     * @version 1.0
     */
    public ResponseMap statistics(StatisticsReqVO vo) {
        String jumpUrl = "";
        String smallprogramId = "";
        String smallprogramPath = "";
        if (StringUtil.isNotBlank(vo.getCmd()) && vo.getCmd().contains("smallprogram")) {
            smallprogramId = vo.getSmallprogramId();
            smallprogramPath = vo.getSmallprogramPath() == null ? "" : vo.getSmallprogramPath();
        } else {
            jumpUrl = vo.getJumpUrl();
        }
        //添加记录
        add(vo.getCmd(), vo.getOpenid(), jumpUrl, smallprogramId, smallprogramPath);
        return ResponseMap.success();
    }
}
